package javacommon.base;

import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.mvc.UrlFilenameViewController;

import com.google.gson.Gson;
import com.smg.community.model.City;
import com.smg.community.model.CrmUser;
import com.smg.community.model.ExtJsPageData;
import com.smg.community.model.SmgIndexTopFiveimages;
import com.smg.community.service.CityManager;
import com.smg.community.service.CrmRightManager;
import com.smg.community.service.CrmRoleManager;
import com.smg.community.service.CrmRoleRightManager;
import com.smg.community.service.CrmUserManager;
import com.smg.community.service.MemMemberManager;
import com.smg.community.service.SmgIndexRightbottomImageManager;
import com.smg.community.service.SmgIndexTopFiveimagesManager;
import com.smg.community.service.SmgKeywordManager;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yushengbing
 * 
 * @param <Entity>
 * @param <PK>
 */
/**
 * @author user
 *
 * @param <Entity>
 * @param <PK>
 */
public class BaseRestSpringController<Entity, PK> extends UrlFilenameViewController implements Serializable
{
	
	@Autowired
	public SmgIndexRightbottomImageManager smgIndexRightbottomImageManager;
	@Autowired
	public SmgIndexTopFiveimagesManager smgIndexTopFiveimagesManager;
	@Autowired
	public CrmUserManager crmUserManager;
	@Autowired
	public CrmRoleManager crmRoleManager;
	@Autowired
	public CrmRightManager crmRightManager;
	@Autowired
	public CrmRoleRightManager crmRoleRightManager;
	@Autowired
	public  CityManager cityManager;
	@Autowired
	public SmgKeywordManager smgKeywordManager;
	@Autowired
	public MemMemberManager memMemberManager;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 获取当前登录用户
	 * */
	public static CrmUser getLoginUser(HttpServletRequest request)
	{
		return (CrmUser) request.getSession().getAttribute("SESSION_USER");
	}
	public void setPagerModel(BaseQuery query, HttpServletRequest request)
	{
		String limit = request.getParameter("limit");
		String start = request.getParameter("start");
		query.setLimit(null == limit ? 25 : Integer.parseInt(limit));
		query.setStart(null == start ? 0 :Integer.parseInt(start) );
	}

	public void writeErrorMessage(HttpServletResponse response)
	{
		writeErrorMessage(response, null);
	}

	public void writeErrorMessage(HttpServletResponse response, Map<String, Object> map)
	{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		if (null == map)
		{
			map = new HashMap<String, Object>();
		}
		map.put("state", -1);
		Writer out;
		try
		{
			out = response.getWriter();
			out.write(new Gson().toJson(map));
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void writeSuccessMessage(HttpServletResponse response)
	{
		writeSuccessMessage(response, null);
	}

	public void writeSuccessMessage(HttpServletResponse response, Map<String, Object> map)
	{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		if (null == map)
		{
			map = new HashMap<String, Object>();
		}
		map.put("state", 1);
		if(map.get("desc") == null){
			map.put("desc", "success");
		}
		Writer out;
		try
		{
			out = response.getWriter();
			out.write(new Gson().toJson(map));
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void writeMessage(Writer out, Object map)
	{
		try
		{
			out.write(new Gson().toJson(map));
			out.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	
	public  ExtJsPageData<Entity>  transToExtJsPageData(List<Entity> resultList,int totalCount) {
		ExtJsPageData<Entity>  extJsPageData = new ExtJsPageData<Entity>();
		extJsPageData.setCount(totalCount);
		extJsPageData.setDesc("success");
		extJsPageData.setRoot(resultList);
		extJsPageData.setState(1);
		return extJsPageData;
	}
	
	public Map<String,String> writeSuccessMessage(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","1");
		map.put("desc","success");
		return map;
	}
	public Map<String,String> writeSuccessMessage(String message){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","1");
		map.put("desc",message);
		return map;
	}
	
	public Map<String,String> writeSuccessMessage(Map<String,String> map){
		if ( map == null){
			map = new HashMap<String,String>();
		}
		map.put("state","1");
		return map;
	}
	
	public Map<String,String> writeErrorMessage(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","-1");
		map.put("desc","error");
		return map;
	}
	
	public Map<String,String> writeErrorMessage(String message){
		Map<String,String> map = new HashMap<String,String>();
		map.put("state","-1");
		map.put("desc",message);
		return map;
	}
	public Map<String,String> writeErrorMessage(Map<String,String> map){
		if ( map == null){
			map = new HashMap<String,String>();
		}
		map.put("state","-1");
		return map;
	}
	
	public void noticeFront(){
		
	}
}
